package com.nuanshui.heatedloan.credit.service;


import com.nuanshui.framework.consts.TransactionManagerType;
import com.nuanshui.framework.security.DESC;
import com.nuanshui.heatedloan.constant.DubboCacheRegion;
import com.nuanshui.heatedloan.constant.ServiceConstant;
import com.nuanshui.heatedloan.credit.dal.mapper.CustomerBankCardInfoMapper;
import com.nuanshui.heatedloan.credit.facade.AllinPayTransService;
import com.nuanshui.heatedloan.credit.facade.BankCardService;
import com.nuanshui.heatedloan.credit.facade.CustomerBankCardInfoService;
import com.nuanshui.heatedloan.credit.facade.CustomerIdCardInfoService;
import com.nuanshui.heatedloan.credit.facade.third.party.RiskManagerInteractionService;
import com.nuanshui.heatedloan.credit.form.CustomerBankCardInfoForm;
import com.nuanshui.heatedloan.credit.request.BankCardRequest;
import com.nuanshui.heatedloan.credit.response.RiskResponse;
import com.nuanshui.heatedloan.credit.vo.CustomerBankCardInfoVO;
import com.nuanshui.heatedloan.dubbo.extension.cache.manager.DubboCacheManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service(ServiceConstant.CUSTOMERBANKCARDINFOSERVICE)
@Transactional(readOnly=true,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
public class CustomerBankCardInfoServiceImpl implements  CustomerBankCardInfoService{

	private  static  final Logger logger= LoggerFactory.getLogger(CustomerBankCardInfoServiceImpl.class);

	@Autowired
	private CustomerBankCardInfoMapper customerBankCardInfoMapper;

	@Autowired
	 private AllinPayTransService allinPayTransService;

	@Autowired
	private CustomerIdCardInfoService customerIdCardInfoService;

	@Autowired
	private RiskManagerInteractionService riskManagerInteractionService;

	@Autowired
	private DubboCacheManager dubboCacheManager;

	@Autowired
	private BankCardService bankCardService;
	
	@Override
	@Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
	public RiskResponse saveCustomerBankCardInfo(CustomerBankCardInfoForm customerBankCardInfoForm) {
		//调用风控的接口，返回是否验证成功
		String cardNum=DESC.decrypt3DES(customerBankCardInfoForm.getMixCardNo());
		//手机号码
		String phoneNum=customerBankCardInfoForm.getMobileNo();
		String userId=customerBankCardInfoForm.getUserId();
		BankCardRequest bankCardRequest=new BankCardRequest();
		bankCardRequest.setBankCardNum(cardNum);
		bankCardRequest.setCustomerId(userId);
		bankCardRequest.setPhoneNum(phoneNum);
		bankCardRequest.setBankCode(customerBankCardInfoForm.getCardBankCode());
		logger.info("=============开始调用风控四要素接口===============");
		RiskResponse riskResponse=riskManagerInteractionService.fourFactorVerification(bankCardRequest);
		if(riskResponse==null){
			riskResponse=new RiskResponse();
			riskResponse.setResultCode("10");
			return riskResponse;
		}
		logger.info("=====调用风控四要素接口完成，结果码为"+riskResponse.getResultCode()+"======");
		//riskResponse.setResultCode("00");
		if("00".equals(riskResponse.getResultCode())){
			riskResponse.setResultCode("00");
			customerBankCardInfoForm.setAuthDesc(riskResponse.getResultMessage());
			CustomerBankCardInfoVO vo = this.loadVOByUserIdAndMixCardNo(userId,customerBankCardInfoForm.getMixCardNo());
			if( vo == null ){
				customerBankCardInfoMapper.saveCustomerBankCardInfo(customerBankCardInfoForm);
			}else {
				customerBankCardInfoForm.setId(vo.getId());
				customerBankCardInfoMapper.updateCustomerBankCardInfo(customerBankCardInfoForm);
			}
			dubboCacheManager.evictCache(DubboCacheRegion.CACHE_CONSTANT_BANKINFO_LIST,userId);
		}else{
			riskResponse.setResultCode("10");
		}
		return riskResponse;
	}


	@Override
	@Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
	public void updateCustomerBankCardInfo(CustomerBankCardInfoForm customerBankCardInfoForm) {
		customerBankCardInfoMapper.updateCustomerBankCardInfo(customerBankCardInfoForm);
	}

	@Override
	public CustomerBankCardInfoVO loadVO(String id) {
		CustomerBankCardInfoVO customerBankCardInfoVO=customerBankCardInfoMapper.loadVO(id);
		if(customerBankCardInfoVO!=null){
			customerBankCardInfoVO.setMixCardNo(DESC.encrypt3DES(customerBankCardInfoVO.getCardNo()));
		}
		return customerBankCardInfoVO;
	}

	@Override
	@Transactional(readOnly=false,value= TransactionManagerType.CREDIT_TRANSACTION_MANAGER)
	public void deleteCustomerBankCardInfo(String id) {
		customerBankCardInfoMapper.deleteCustomerBankCardInfo(id);
	}

	public List<CustomerBankCardInfoVO> loadCustomerBankCardInfoVOByUserId(String userId){
		return  customerBankCardInfoMapper.loadCustomerBankCardInfoVOByUserId(userId);
	}

	@Override
	public CustomerBankCardInfoVO loadVOByObject(CustomerBankCardInfoForm customerBankCardInfoForm) {
		return customerBankCardInfoMapper.loadVOByObject(customerBankCardInfoForm);
	}

	@Override
	public CustomerBankCardInfoVO loadLatestVOByUserId(String userId) {
		return customerBankCardInfoMapper.loadLatestVOByUserId(userId);
	}

	@Override
	public int loadCountByUserId(String userId) {
		return customerBankCardInfoMapper.loadCountByUserId(userId);
	}

	@Override
	public CustomerBankCardInfoVO loadVOByUserIdAndMixCardNo(String userId, String mixCardNo) {
		return customerBankCardInfoMapper.loadVOByUserIdAndMixCardNo(userId,mixCardNo);
	}

	@Override
	public CustomerBankCardInfoVO loadVOByMixCardNo(String mixCardNo) {
		return customerBankCardInfoMapper.loadVOByMixCardNo(mixCardNo);
	}
}
